//
//  RecommendViewController.m
//  Contractor
//
//  Created by 金鑫 on 16/5/3.
//  Copyright © 2016年 star j. All rights reserved.
//

#import "RecommendViewController.h"
#import "CONST.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"

@interface RecommendViewController ()

@end

@implementation RecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor] ;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.edgesForExtendedLayout = UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight;

    //创建二维码图片
    [self getToken] ;
}

#pragma mark - token-Get-Download
- (void)getToken
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:access_token_GET parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil] ;
        if ([result isKindOfClass:[NSDictionary class]]) {
            NSLog(@"resultGet:%@",result) ;
            NSString *token = result[@"access_token"] ;
            [self PostTicketWithToken:token] ;
        }
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        
    }] ;
}

#pragma mark - ticket-POST-Download
- (void)PostTicketWithToken:(NSString *)token
{
    NSString *postUrl = [NSString stringWithFormat:@"https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%@",token] ;
    NSLog(@"postUrl:%@",postUrl) ;
    NSString *scene_str = [NSString stringWithFormat:@"%@|%@|%@|%@",_model.uid,_model.nickname,_model.authgroup,_model.realname] ;
    NSDictionary *scene = @{@"scene_str":scene_str} ;
    NSDictionary *action_info = @{@"scene":scene} ;
    NSDictionary *parameters = @{@"action_name":@"QR_LIMIT_STR_SCENE",@"action_info":action_info};
//    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil] ;
//    
//    NSString *jsonstr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] ;
//    NSLog(@"jsonstr:%@",jsonstr) ;
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer] ;
    
    [manager POST:postUrl parameters:parameters success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil] ;
        if ([result isKindOfClass:[NSDictionary class]]) {
            NSLog(@"resultPost:%@",result) ;
            //二维码图片
             NSString *barcodeUrl = [[NSString alloc] initWithFormat:@"%@",result[@"ticket"]] ;
            UIImageView *barcodeView = [[UIImageView alloc] initWithFrame:CGRectMake(__kScreenWidth/4, (self.view.frame.size.height - __kScreenWidth/2) / 2, __kScreenWidth/2, __kScreenWidth/2)] ;
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%@",barcodeUrl]] ;
            [barcodeView sd_setImageWithURL:url] ;
            [self.view addSubview:barcodeView] ;
            
            //创建扫一扫lable
            UILabel *barcodeLabel = [[UILabel alloc] init] ;
            barcodeLabel.font = [UIFont fontWithName:@"AvenirLTStd-Light" size:16.0f] ;
            barcodeLabel.backgroundColor = [UIColor clearColor] ;
            barcodeLabel.text = @"微信扫一扫，享受更多优惠。" ;
            float labelY = barcodeView.frame.origin.y + barcodeView.frame.size.height + 20 ;
            barcodeLabel.frame = CGRectMake(0, labelY, __kScreenWidth, 18.0f) ;
            barcodeLabel.textAlignment = NSTextAlignmentCenter ;
            [self.view addSubview:barcodeLabel] ;
        }

    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        
    }] ;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO ;
    self.navigationItem.title = @"我的二维码" ;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES ;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
